package com.daxt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.daxt.base.common.util.RedisUtil;
import com.daxt.common.result.Result;
import com.daxt.common.util.DateUtil;
import com.daxt.common.util.HttpClientUtil;
import com.daxt.mapper.accept.*;
import com.daxt.mapper.archives.ArchivesDocFileMapper;
import com.daxt.mapper.archives.ArchivesDocMapper;
import com.daxt.mapper.sys.ArchivesClassMapper;
import com.daxt.mapper.sys.ArchivesFondsMapper;
import com.daxt.mapper.sys.ArchivesTypeMapper;
import com.daxt.mapper.sys.SystemConfigMapper;
import com.daxt.model.dic.SystemConfigType;
import com.daxt.model.dic.VersionsMarkType;
import com.daxt.model.service.accept.vo.*;
import com.daxt.model.service.archives.vo.*;
import com.daxt.model.service.base.vo.SystemConfig;
import com.daxt.service.ArchivesAcceptService;
import com.daxt.service.MountService;
import com.daxt.service.UploadService;
import com.daxt.utils.AseEncryptUtil;
import com.daxt.utils.FileValidate;
import com.daxt.utils.file.DownFileUtil;
import com.daxt.utils.file.FileBaseUtil;
import com.daxt.utils.file.PdfUtils;
import com.daxt.utils.file.ToPdfUtil;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import edu.usc.ir.sentiment.analysis.cmdline.TikaTool;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.lang.reflect.Array;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class ArchivesAcceptServiceImpl implements ArchivesAcceptService {

    @Autowired
    private ColumnsMapper columnsMapper;
    @Autowired
    private MateralInfoMapper materalInfoMapper;
    @Autowired
    private BusinessInfoMapper businessInfoMapper;
    @Autowired
    private ArchivesDocMapper archivesDocMapper;
    @Autowired
    private MountService mountService;
    @Autowired
    private UploadService uploadService;
    @Autowired
    private FormDataMapper formDataMapper;
    @Autowired
    private ArchivesDocFileMapper fileMapper;
    @Autowired
    private SystemConfigMapper systemConfigMapper;
    @Autowired
    private ArchivesClassMapper archivesClassMapper;
    @Autowired
    private ArchivesTypeMapper archivesTypeMapper;
    @Autowired
    private ArchivesFondsMapper archivesFondsMapper;

    @Autowired
    private ArchivesAcceptLogMapper acceptLogMapper;

    @Autowired
    private AcceptItemClassMapper acceptItemClassMapper;

    @Override
    public void sendFlow(String flowId, String categoryId, String categoryName, String creatorLoginName, String createTime, String flowName, String orgId, String orgName) {
        String url="api/v1/process_sync/init";
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("flowId",flowId);
        hashMap.put("categoryId",categoryId);
        hashMap.put("categoryName",categoryName);
        hashMap.put("creatorLoginName",creatorLoginName);
        hashMap.put("createTime",createTime.toString());
        hashMap.put("flowName",flowName);
        hashMap.put("orgId",orgId);
        hashMap.put("orgName",orgName);
        //String resultStr = HttpClientUtil.doPost(url,hashMap);

        String av = RedisUtil.getSysConfig(SystemConfigType.PROFILES_ACTIVE);
        if(av.equals("lq")){

        }

        System.out.println("发送流程：流程id:\n"+flowId+"  分类id:\n  "+categoryId+" 分类名:\n "+categoryName+"  创建人登录名:\n  "+creatorLoginName
                +"  创建时间:\n  "+createTime+"  流程名称:\n  "+flowName+" 部门id:\n  "+orgId+" 部门名称:\n "+orgName);

    }

    @Override
    public void receiveFlow(String flowId, String nodeId, String nodeName, String auditLoginName, String reciveTime) {
        String url="api/v1/process_sync/recive";
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("flowId",flowId);
        hashMap.put("nodeId",nodeId);
        hashMap.put("nodeName",nodeName);
        hashMap.put("auditLoginName",auditLoginName);
        hashMap.put("reciveTime",reciveTime.toString());
        //String resultStr = HttpClientUtil.doPost(url,hashMap);
        String av = RedisUtil.getSysConfig(SystemConfigType.PROFILES_ACTIVE);
        if(av.equals("lq")){

        }
        System.out.println("接受流程：流程id:"+flowId+" 节点id:"+nodeId+"  节点名称:"+nodeName+"  接收人登录名:"+auditLoginName+"  接受时间:"+reciveTime);
    }

    @Override
    public void updateFlow(String flowId, String nodeId, String nodeName, String auditLoginName, String auditTime, String flowStatus) {
        String url="api/v1/process_sync/update";
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("flowId",flowId);
        hashMap.put("nodeId",nodeId);
        hashMap.put("nodeName",nodeName);
        hashMap.put("auditLoginName",auditLoginName);
        hashMap.put("auditTime",auditTime.toString());
        hashMap.put("flowStatus",flowStatus);
        String av = RedisUtil.getSysConfig(SystemConfigType.PROFILES_ACTIVE);
        if(av.equals("lq")){

        }
        //String resultStr = HttpClientUtil.doPost(url,hashMap);
        System.out.println("审批流程：流程id:"+flowId+" 节点id:"+nodeId+"  节点名称:"+nodeName+"  审批人登录名:"+auditLoginName+"  审批时间:"+auditTime +"  流程状态："+flowStatus);
    }

    /**
     * 同步质保资料分类树
     */
    @Override
    @Scheduled(cron = "0 0 22 ? * SUN")
    public void syncZBClass(){
        if(RedisUtil.isAppoint(VersionsMarkType.VERSIONS_5)){
            List<String> list=new ArrayList<>();
            list.add("111");
            list.add("112");
            list.add("113");
            ArchivesFonds archivesFonds = archivesFondsMapper.selectOne(new QueryWrapper<ArchivesFonds>().like("fondsName","离隰"));
            QueryWrapper<ArchivesType> typeQueryWrapper= new QueryWrapper<>();
            typeQueryWrapper.eq("typeName","项目建设类");
            typeQueryWrapper.eq("fondsId",archivesFonds.getId());
            ArchivesType archivesType= archivesTypeMapper.selectOne(typeQueryWrapper);
            QueryWrapper<ArchivesClass> classQueryWrapper=new QueryWrapper<>();
            classQueryWrapper.eq("typeId",archivesType.getId());
            classQueryWrapper.eq("parentId","0");
            classQueryWrapper.eq("className","施工");
            ArchivesClass archivesClass= archivesClassMapper.selectOne(classQueryWrapper);

            for(String str : list){
                getZBClass(str,archivesFonds,archivesType,archivesClass);
            }
            ArchivesAcceptLog acceptLog=new ArchivesAcceptLog();
            acceptLog.setDescribes("ZB");
            acceptLog.setCreateTime(DateUtil.getCurrentTime());
            acceptLogMapper.insert(acceptLog);
        }
    }


    /**
     * 获取保资料系统的分类树
     */
    @Transactional(rollbackFor = Exception.class)
    public void getZBClass(String orgID,ArchivesFonds archivesFonds,ArchivesType archivesType,ArchivesClass archivesClass){
        QueryWrapper<ArchivesAcceptLog> logQueryWrapper=new QueryWrapper<>();
        logQueryWrapper.orderByDesc("id");
        logQueryWrapper.last("limit 1");
        ArchivesAcceptLog log = acceptLogMapper.selectOne(logQueryWrapper);

        String url="http://fjhmtd.com:32504/glaf//website/ws/dataset/api/json";
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("id","2873dab868964b808142bffd884cbcd0");
        if(log==null){
            hashMap.put("TIME_","2000-01-01 00:00:00");
        }else{
            hashMap.put("TIME_",log.getCreateTime());
        }
        hashMap.put("page","1");
        hashMap.put("rows","10000");
        hashMap.put("org_ID",orgID);


        String resultStr = HttpClientUtil.doPost(url,hashMap);
        JSONObject jsonObj= JSON.parseObject(resultStr);
        JSONArray rows = new JSONArray(jsonObj.getJSONArray("rows"));
        if(rows.size()<=0){
            return;
        }

        ArrayList<ZbWbsTree> ZbList0=new ArrayList<>();
        ArrayList<ZbWbsTree> ZbList1=new ArrayList<>();
        ArrayList<ZbWbsTree> ZbList2=new ArrayList<>();
        ArrayList<ZbWbsTree> ZbList3=new ArrayList<>();
        //ArrayList<ZbWbsTree> ZbList4=new ArrayList<>();
        for(int i=0;i<rows.size();i++){
            ZbWbsTree zbWbsTree = JSON.parseObject(rows.get(i).toString(),ZbWbsTree.class);
            int delFlag =zbWbsTree.getDelFlag();
            int treeLevel =zbWbsTree.getTreeLevel();
            if(delFlag==1 || treeLevel==5 || treeLevel==6 || treeLevel==3 ){
                continue;
            }
            if(treeLevel == -1){
                ZbList0.add(zbWbsTree);
            }
            if(treeLevel == 0){
                ZbList1.add(zbWbsTree);
            }
            if(treeLevel == 1){
                ZbList2.add(zbWbsTree);
            }
            if(treeLevel == 2){
                ZbList3.add(zbWbsTree);
            }
            /*if(treeLevel == 4){
                ZbList4.add(zbWbsTree);
            }*/
        }

        for (ZbWbsTree zbWbsTree:ZbList0){

            int count = archivesClassMapper.selectCount(new QueryWrapper<ArchivesClass>().eq("extId",zbWbsTree.getId()));
            if(count <=0){

                ArchivesClass entity=new ArchivesClass();
                entity.setExtId(zbWbsTree.getId());
                entity.setClassName(zbWbsTree.getIndexName());
                entity.setFondsId(archivesFonds.getId().toString());
                entity.setTypeId(archivesType.getId().toString());
                entity.setIsCatalog("1");
                entity.setClassType("1");
                entity.setParentId(archivesClass.getId());
                entity.setTemplate("dataList");
                if(orgID.equals("111")){
                    entity.setSort(8);
                    entity.setClassNum("08");
                    entity.setTreeStr(archivesClass.getTreeStr()+"·08");
                }else if (orgID.equals("112")){
                    entity.setSort(9);
                    entity.setClassNum("09");
                    entity.setTreeStr(archivesClass.getTreeStr()+"·09");
                }else if (orgID.equals("113")){
                    entity.setSort(10);
                    entity.setClassNum("10");
                    entity.setTreeStr(archivesClass.getTreeStr()+"·10");
                }

                archivesClassMapper.insert(entity);
            }
        }

        /*for (ZbWbsTree zbWbsTree:ZbList1){
            int count = archivesClassMapper.selectCount(new QueryWrapper<ArchivesClass>().eq("extId",zbWbsTree.getId()));
            if(count <=0){
                createZbClass(archivesType.getId().toString(),archivesFonds.getId().toString(),zbWbsTree);
            }
        }*/

        /*for (ZbWbsTree zbWbsTree:ZbList2){
            int count = archivesClassMapper.selectCount(new QueryWrapper<ArchivesClass>().eq("extId",zbWbsTree.getId()));
            if(count <=0){
                createZbClass(archivesType.getId().toString(),archivesFonds.getId().toString(),zbWbsTree);
            }
        }*/

        /*for (ZbWbsTree zbWbsTree:ZbList3){
            int count = archivesClassMapper.selectCount(new QueryWrapper<ArchivesClass>().eq("extId",zbWbsTree.getId()));
            if(count <=0){
                createZbClass(archivesType.getId().toString(),archivesFonds.getId().toString(),zbWbsTree);
            }
        }*/


        //for (ZbWbsTree zbWbsTree:ZbList4){
        //    int count = archivesClassMapper.selectCount(new QueryWrapper<ArchivesClass>().eq("extId",zbWbsTree.getId()));
        //    if(count <=0){
        //        createZbClass(archivesType.getId().toString(),archivesFonds.getId().toString(),zbWbsTree);
        //    }
        //}


    }

    @Override
    public String getWsAccept(String startTime,String endTime) {
        SystemConfig systemConfig = systemConfigMapper.selectOne(new QueryWrapper<SystemConfig>().eq("sysKey","WS_ACCEPT_URL"));
        String wsUrl= systemConfig.getSysValue();
        System.out.println("------------------------------------------------------开始调用接口-------------------------------------------------------------------------");
        //String url="web/approval/getFinishedBusinessInfo";
        String url=wsUrl+"web/approval/getFinishedBusinessInfo";
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("page","1");
        hashMap.put("rows","1");
        hashMap.put("agentCode","1411219999");

        if(StringUtils.isEmpty(startTime)&&StringUtils.isEmpty(endTime)){
            startTime=DateUtil.getCurrentTime(DateUtil.DateFormat.YYYY_MM_DD);
            endTime=DateUtil.addDayOfMonth(startTime, DateUtil.DateFormat.YYYY_MM_DD,1);

        }
        //String startTime="2022-09-01";
        //String endTime="2022-09-06";
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);


        QueryWrapper<AcceptItemClass> classQueryWrapper=new QueryWrapper<>();
        classQueryWrapper.select("distinct className");
        classQueryWrapper.ne("className","独立");
        List<AcceptItemClass> itemClassList = acceptItemClassMapper.selectList(classQueryWrapper);

        ArrayList<String> classNameList=new ArrayList<>();
        for(AcceptItemClass itemClass:itemClassList){
            classNameList.add(itemClass.getClassName());
        }

        //classNameList.add("农业");
        //classNameList.add("林业");
        //classNameList.add("水利");
        //classNameList.add("行政许可类");
        //classNameList.add("行政确认类");
        //classNameList.add("其他权力类");
        //classNameList.add("投资项目");
        //classNameList.add("民政类");
        //classNameList.add("财政类");
        //classNameList.add("流通");
        //classNameList.add("餐饮");
        //classNameList.add("小作坊");
        //classNameList.add("特种设备");
        //classNameList.add("小经营备案");
        //classNameList.add("计量类");
        //classNameList.add("运政管理类");
        //classNameList.add("客运管理类");
        //classNameList.add("公路水运类");
        //classNameList.add("永久");
        //classNameList.add("20年");
        //classNameList.add("10年");
        //classNameList.add("30年");


        //判断有无对应分类
        QueryWrapper<ArchivesClass> parentWrapper=new QueryWrapper<>();
        parentWrapper.eq("fondsId","20");
        parentWrapper.eq("typeId","17");
        parentWrapper.eq("parentId","0");
        parentWrapper.eq("className",year);
        ArchivesClass parentClass =  archivesClassMapper.selectOne(parentWrapper);

        if(parentClass == null || parentClass.getId()==null){
            //分类不存在，请添加分类
            return "201";
        }

        QueryWrapper<ArchivesClass> wrapper=new QueryWrapper<>();
        wrapper.eq("fondsId","20");
        wrapper.eq("typeId","17");
        wrapper.like("treeStr",parentClass.getClassNum()+"·_·");
        wrapper.in("className",classNameList);
        List<ArchivesClass> classList =  archivesClassMapper.selectList(wrapper);

        if(classList.size() != classNameList.size()){
            //分类不存在，请添加分类
            return "201";
        }

        Map<String,Long> classMap=new HashMap<>();
        for(ArchivesClass archivesClass:classList){
            classMap.put(archivesClass.getClassName(),archivesClass.getId());
        }



        hashMap.put("finishedStartTime",startTime);
        hashMap.put("finishedEndTime",endTime);
        //获取办结办件分页结果
        String resultStr = HttpClientUtil.doPost(url,hashMap);
        JSONObject jsonObj= JSON.parseObject(resultStr);
        if(!jsonObj.get("code").toString().equals("200")){
            System.out.println("getFinishedBusinessInfo接口调用失败原因:"+jsonObj.get("msg"));
            return jsonObj.get("code").toString();
        }
        String rows= jsonObj.get("totalRow").toString();
        int totalRow=Integer.parseInt(rows);
        int page=totalRow/50;
        if(totalRow%50!=0){
            page++;
        }
        String result="200";
        for (int i=1;i<=page;i++){
            result=getFinishedBusinessInfo(i+"",wsUrl,startTime,endTime,classMap);
        }
        System.out.println("--------------------------------------------------------------------调用完毕-----------------------------------------------------");

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String getFinishedBusinessInfo(String page,String wsUrl,String startTime,String endTime,Map<String,Long> classMap) {
        String url=wsUrl+"web/approval/getFinishedBusinessInfo";
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("page",page);
        hashMap.put("rows","50");
        hashMap.put("agentCode","1411219999");
        hashMap.put("finishedStartTime",startTime);
        hashMap.put("finishedEndTime",endTime);
        /*hashMap.put("itemType",accept.getItemType());
        hashMap.put("assort",accept.getAssort());
        hashMap.put("regionCode",accept.getRegionCode());
        hashMap.put("itemCode",accept.getItemCode());*/


        //获取办结办件的分页结果
        String resultStr = HttpClientUtil.doPost(url,hashMap);
        System.out.println(DateUtil.getStartOfDay(new Date()));
        System.out.println(DateUtil.getEndOfDay(new Date()));
        System.out.println(new Date());
        //解析办结办件的分页结果
        String result = jsonResolverBi(resultStr,wsUrl,classMap);


        return result;
    }

    //解析办结办件分页结果的json数据
    public String jsonResolverBi(String jsonStr,String wsUrl,Map<String,Long> classMap){
        JSONObject jsonObj= JSON.parseObject(jsonStr);

        if(!jsonObj.get("code").toString().equals("200")){
            System.out.println("getFinishedBusinessInfo接口调用失败原因:"+jsonObj.get("msg"));
            return jsonObj.get("code").toString();
        }

        JSONArray jsonArray= jsonObj.getJSONArray("pageList");

        for(int i=0;i<jsonArray.size();i++){
            jsonObj= jsonArray.getJSONObject(i);
            jsonObj= jsonObj.getJSONObject("columns");

            String  orgName=jsonObj.get("ORG_NAME")==null? null:jsonObj.get("ORG_NAME").toString();
            String  regionCode=jsonObj.get("REGION_CODE")==null? null:jsonObj.get("REGION_CODE").toString();
            String  applySubject=jsonObj.get("APPLY_SUBJECT")==null? null:jsonObj.get("APPLY_SUBJECT").toString();
            String  applicant=jsonObj.get("APPLICANT")==null? null:jsonObj.get("APPLICANT").toString();
            String  agentName=jsonObj.get("AGENT_NAME")==null? null:jsonObj.get("AGENT_NAME").toString();
            String  itemId=jsonObj.get("ITEM_ID")==null? null:jsonObj.get("ITEM_ID").toString();
            String  state=jsonObj.get("STATE")==null? null:jsonObj.get("STATE").toString();
            String  itemCode=jsonObj.get("ITEM_CODE")==null? null:jsonObj.get("ITEM_CODE").toString();
            String  agentCode=jsonObj.get("AGENT_CODE")==null? null:jsonObj.get("AGENT_CODE").toString();
            String  itemName=jsonObj.get("ITEM_NAME")==null? null:jsonObj.get("ITEM_NAME").toString();
            String  rowNum=jsonObj.get("ROWNUM_")==null? null:jsonObj.get("ROWNUM_").toString();
            String  assort=jsonObj.get("ASSORT")==null? null:jsonObj.get("ASSORT").toString();
            String  submitTime=jsonObj.get("SUBMIT_TIME")==null? null:jsonObj.get("SUBMIT_TIME").toString();
            String  finishTime=jsonObj.get("FINISH_TIME")==null? null:jsonObj.get("FINISH_TIME").toString();
            String  regionName=jsonObj.get("REGION_NAME")==null? null:jsonObj.get("REGION_NAME").toString();
            String  orgCode=jsonObj.get("ORG_CODE")==null? null:jsonObj.get("ORG_CODE").toString();
            String  acceptUserCode=jsonObj.get("ACCEPT_USER_CODE")==null? null:jsonObj.get("ACCEPT_USER_CODE").toString();
            String  timeLimit=jsonObj.get("TIME_LIMIT")==null? null:jsonObj.get("TIME_LIMIT").toString();
            String  receiveNumber=jsonObj.get("RECEIVE_NUMBER")==null? null:jsonObj.get("RECEIVE_NUMBER").toString();//办件编号
            String  bsNum=jsonObj.get("BSNUM")==null? null:jsonObj.get("BSNUM").toString();
            String  type=jsonObj.get("TYPE")==null? null:jsonObj.get("TYPE").toString();
            String  acceptUserName=jsonObj.get("ACCEPT_USER_NAME")==null? null:jsonObj.get("ACCEPT_USER_NAME").toString();

            //去重复操作
            QueryWrapper columnsWrapper=new QueryWrapper();
            columnsWrapper.eq("receiveNumber",receiveNumber);
            int columnsRow = columnsMapper.selectCount(columnsWrapper);

            //若档案系统中存在，则跳过此数据
            if(columnsRow>0){
                continue;
            }

            //判断办结状态是否为99，如果不是则跳过本条数据
            if(!"99".equals(state) && !"90".equals(state)){
                continue;
            }

            QueryWrapper<AcceptItemClass> classQueryWrapper=new QueryWrapper<>();
            classQueryWrapper.eq("itemName",itemName);
            AcceptItemClass acceptItemClass = acceptItemClassMapper.selectOne(classQueryWrapper);

            //如果有独立档案系统则不入本系统
            if(acceptItemClass != null && "独立".equals(acceptItemClass.getClassName())){
                continue;
            }


            String createTime=DateUtil.stampToTime(System.currentTimeMillis()+"");
            String startTime= DateUtil.stampToTime(submitTime);
            String endTime=DateUtil.stampToTime(finishTime);
            String year=DateUtil.getYear(endTime);
            timeLimit=DateUtil.stampToTime(timeLimit);


            //columnsMapper.insert(columns);

            ArchivesDoc  archivesDocDossier=new ArchivesDoc();
            archivesDocDossier.setDossierId(0L);//
            //archivesDoc.setDossierNum("");//案卷号
            //archivesDoc.setArchivalNum("");//档号
            //archivesDoc.setFondsId("");//全宗ID
            //archivesDoc.setTypeId("");//门类ID
            //archivesDoc.setClassId("");//分类ID


            archivesDocDossier.setArchivalYear(year);//年度
            //archivesDoc.setStoragePeriod();//保管期限
            archivesDocDossier.setTitle(applySubject);//题名
            archivesDocDossier.setPages("0");//页数                   后期处理
            archivesDocDossier.setDutyPeople(acceptUserName);//责任者
            archivesDocDossier.setSecret("0");//密级
            //archivesDoc.setRemark();//备注
            //archivesDoc.setArchivesCode();//档案馆代码

            if(acceptItemClass == null || StringUtils.isEmpty(acceptItemClass.getClassName())){
                archivesDocDossier.setTypeId(0L);
            }else{
                archivesDocDossier.setTypeId(17L);
                Long classId = classMap.get(acceptItemClass.getClassName());
                archivesDocDossier.setClassId(classId);
                archivesDocDossier.setFondsId("20");
            }



            archivesDocDossier.setState("6");//状态 0 整理 1归档审批 2归档 3 鉴定 4销毁 5回收站
            archivesDocDossier.setFileType("dossier");//doc 件  dossier卷
            archivesDocDossier.setStartTime(startTime);//受理时间
            //archivesDoc.setDocFrom();//档案来源             后期处理
            archivesDocDossier.setEndTime(endTime);
            archivesDocDossier.setApplicant(applicant);
            archivesDocDossier.setItemCode(itemCode);
            archivesDocDossier.setItemName(itemName);
            archivesDocDossier.setAssort(assort);//需要处理
            archivesDocDossier.setOrgCode(orgCode);
            archivesDocDossier.setTimeLimit(timeLimit);
            archivesDocDossier.setType(getTypeName(type));
            archivesDocDossier.setReceiveNumber(receiveNumber);
            //archivesDoc.setDossierNum("");    //                后期处理
            archivesDocDossier.setItemNum("1");  //件数            后期处理
            archivesDocDossier.setAcceptUserName(acceptUserName);//外部文件办理人



            archivesDocMapper.insert(archivesDocDossier);
            Columns columns=new Columns(archivesDocDossier.getId().toString(),orgName,regionCode,applySubject,agentName,itemId,state,itemCode,
                    agentCode,itemName,rowNum,assort,submitTime,finishTime,regionName,orgCode,acceptUserCode,
                    timeLimit,receiveNumber,bsNum,type,acceptUserName);
            columnsMapper.insert(columns);

            //获取办件基础信息、表单数据、材料数据的数据
            String url=wsUrl+"web/approval/getBusinessInfoAndFormDataAndMateralInfo";
            HashMap<String,String> hashMap=new HashMap<>();
            hashMap.put("receiveNumber",receiveNumber);
            String resultStr = HttpClientUtil.doPost(url,hashMap);
            //解析办件基础信息、表单数据、材料数据的数据
            if(resultStr!=null){
                String pageAndDocNum=jsonResolverBiFdMi(resultStr,archivesDocDossier,wsUrl);
                if(!pageAndDocNum.contains(",")){
                    return pageAndDocNum;
                }

                String page=pageAndDocNum.split(",")[0];
                String docNum=pageAndDocNum.split(",")[1];
                archivesDocDossier.setPages(page);//设置页数
                archivesDocDossier.setItemCount(docNum);//设置件数
                archivesDocMapper.updateById(archivesDocDossier);
            }
        }

        return "200";
    }

    //获解析取办件基础信息、表单数据、材料数据的json数据
    public String jsonResolverBiFdMi(String jsonStr,ArchivesDoc archivesDocDossier,String wsUrl){
        JSONObject jsonObj= JSON.parseObject(jsonStr);

        if(!jsonObj.get("code").toString().equals("200")){
            System.out.println("getFinishedBusinessInfo接口调用失败原因:"+jsonObj.get("msg"));
            return jsonObj.get("code").toString();
        }

        int pages=0;
        int docNum=0;
        //获取json中materalInfo材料数据
        JSONArray jsonArrayMateralInfo= jsonObj.getJSONArray("materalInfo");
        if(!jsonArrayMateralInfo.isEmpty()){
            for(int i=0;i<jsonArrayMateralInfo.size();i++){
                JSONObject json= jsonArrayMateralInfo.getJSONObject(i);

                String  id= json.get("ID")==null? null:json.get("ID").toString();
                String  bsNum= json.get("BSNUM")==null? null:json.get("BSNUM").toString();
                String  uploadTime= json.get("UPLOAD_TIME")==null? null:json.get("UPLOAD_TIME").toString();
                String  operatorId= json.get("OPERATOR_ID")==null? null:json.get("OPERATOR_ID").toString();
                String  operatorName= json.get("OPERATOR_NAME")==null? null:json.get("OPERATOR_NAME").toString();
                String  documentId= json.get("DOCUMENT_ID")==null? null:json.get("DOCUMENT_ID").toString();
                String  fileName= json.get("FILE_NAME")==null? null:json.get("FILE_NAME").toString();
                String  type= json.get("TYPE")==null? null:json.get("TYPE").toString();
                String  filePath= json.get("FILE_PATH")==null? null:json.get("FILE_PATH").toString();
                String  documentName= json.get("DOCUMENT_NAME")==null? null:json.get("DOCUMENT_NAME").toString();
                String  chmod= json.get("CHMOD")==null? null:json.get("CHMOD").toString();
                String  encryption= json.get("ENCRYPTION")==null? null:json.get("ENCRYPTION").toString();


                if(fileName!=null&&fileName.contains("\t")){
                    fileName=fileName.replace("\t","");
                }

                //若文件名称太长
                if(fileName.length()>80&&fileName.contains("（")&&fileName.contains("）")){
                    StringBuilder s=new StringBuilder(fileName);
                    fileName=s.replace(s.indexOf("（"),s.lastIndexOf("）")+1,"").toString();
                }

                ArchivesDoc  archivesDoc=new ArchivesDoc();
                archivesDoc.setDossierId(archivesDocDossier.getId());//
                //archivesDoc.setDossierNum("");
                //archivesDoc.setArchivalNum();//档号
                //archivesDoc.setFondsId();//全宗ID
                //archivesDoc.setTypeId();//门类ID
                //archivesDoc.setClassId();//分类ID
                //archivesDoc.setArchivalYear(archivesDocDossier.getArchivalYear());//年度
                //archivesDoc.setStoragePeriod();//保管期限

                //archivesDoc.setPages("");//页数
                //archivesDoc.setDutyPeople(archivesDocDossier.getDutyPeople());//责任者
                archivesDoc.setSecret("0");//密级
                //archivesDoc.setRemark();//备注
                //archivesDoc.setArchivesCode("");//档案馆代码
                archivesDoc.setTypeId(archivesDocDossier.getTypeId());
                archivesDoc.setClassId(archivesDocDossier.getClassId());
                archivesDoc.setFondsId(archivesDocDossier.getFondsId());
                archivesDoc.setState("6");//状态 0 整理 1归档审批 2归档 3 鉴定 4销毁 5回收站
                archivesDoc.setFileType("doc");//doc 件  dossier卷
                //archivesDoc.setStartTime(archivesDocDossier.getStartTime());//受理时间
                if(i<9){
                    String itemNum="00"+(i+1);
                    archivesDoc.setItemNum(itemNum);
                }else if(i<99){
                    String itemNum="0"+(i+1);
                    archivesDoc.setItemNum(itemNum);
                }else{
                    archivesDoc.setItemNum(""+(i+1));
                }
                //archivesDoc.setAcceptUserName(archivesDocDossier.getAcceptUserName());

                if("1".equals(type)){   //电子材料处理
                    String fileTitle=fileName.substring(0,fileName.lastIndexOf(".")).trim();//获取文件题名
                    archivesDoc.setTitle(documentName);//题名
                    //archivesDoc.setDocFrom("电子文件");//档案来源
                    //文件保存地址
                    String ossDir = RedisUtil.getSysConfig(SystemConfigType.OSS_DIR);
                    String savePath=ossDir+"wsDownload/"+archivesDocDossier.getId();
                    //String savePath="D:/home/jar/ws/"+id+"/";
                    //下载文件
                    downloadFile(filePath,fileName,savePath);
                    //获取文件后缀
                    int page=1;
                    String extName=FileBaseUtil.getFileNameExt(fileName);
                    if("doc".equals(extName)||"docx".equals(extName)){
                        //件word文件转换为pdf
                        ToPdfUtil.doc2pdf(savePath+"/"+fileName, savePath+"/"+fileTitle+".pdf");
                        boolean isFile= getvalidate(new File(savePath+"/"+fileTitle+".pdf"));
                        if(isFile){
                            page= PdfUtils.getPdfPageCount(savePath+"/"+fileTitle+".pdf");
                        }else{
                            page=0;
                        }

                    }else if("xls".equals(extName)||".xlsx".equals(extName)){
                        ToPdfUtil.excel2pdf(savePath+"/"+fileName, savePath+"/"+fileTitle+".pdf");
                        boolean isFile= getvalidate(new File(savePath+"/"+fileTitle+".pdf"));
                        if(isFile){
                            page= PdfUtils.getPdfPageCount(savePath+"/"+fileTitle+".pdf");
                        }else{
                            page=0;
                        }
                    }else if("pdf".equals(extName)){
                        boolean isFile= getvalidate(new File(savePath+"/"+fileName));
                        if(isFile){
                            page= PdfUtils.getPdfPageCount(savePath+"/"+fileName);
                        }else{
                            page=0;
                        }
                    }
                    archivesDoc.setPages(page+"");
                    archivesDoc.setSource("电子文件");
                    //上传附件
                    upLoad(savePath+"/"+fileName,archivesDoc);
                    //删除下载的附件
                    /*File file=new File(savePath+"/"+fileName);
                    File filePdf=new File(savePath+"/"+fileTitle+".pdf");

                    if (file.exists()){
                        file.delete();
                    }
                    if (filePdf.exists()){
                        filePdf.delete();
                    }*/

                    MateralInfo materalInfo=new MateralInfo(archivesDoc.getId().toString(),id,bsNum,uploadTime,operatorId,operatorName
                            ,documentId,fileName,type,filePath,documentName,chmod,encryption);
                    materalInfoMapper.insert(materalInfo);
                    pages=pages+page;
                }else if("0".equals(type)){ //纸质材料处理
                    archivesDoc.setTitle(documentName);
                    //archivesDoc.setDocFrom("纸质");//档案来源
                    archivesDoc.setPages("0");//页数
                    archivesDoc.setSource("纸质文件");
                    archivesDocMapper.insert(archivesDoc);
                    MateralInfo materalInfo=new MateralInfo(archivesDoc.getId().toString(),id,bsNum,uploadTime,operatorId,operatorName
                            ,documentId,fileName,type,filePath,documentName,chmod,encryption);
                    materalInfoMapper.insert(materalInfo);
                    //pages=pages+1;

                }
                docNum++;
            }
        }


        //获取json中formData表单数据
        JSONObject  jsonFormData= jsonObj.getJSONObject("formData");
        if(!jsonFormData.isEmpty()){
            String beiZhu = jsonFormData.get("BeiZhu")==null? null:jsonFormData.get("BeiZhu").toString();
            String lianXiRenYouXiang = jsonFormData.get("LianXiRenYouXiang")==null? null:jsonFormData.get("LianXiRenYouXiang").toString();
            String banJianShuLiang = jsonFormData.get("BanJianShuLiang")==null? null:jsonFormData.get("BanJianShuLiang").toString();
            String lianXiRenDianHua = jsonFormData.get("LianXiRenDianHua")==null? null:jsonFormData.get("LianXiRenDianHua").toString();
            String lianXiRenZhengJianHaoMa = jsonFormData.get("LianXiRenZhengJianHaoMa")==null? null:jsonFormData.get("LianXiRenZhengJianHaoMa").toString();
            String lianXiRenXingMing = jsonFormData.get("LianXiRenXingMing")==null? null:jsonFormData.get("LianXiRenXingMing").toString();
            String youZhengBianMa = jsonFormData.get("YouZhengBianMa")==null? null:jsonFormData.get("YouZhengBianMa").toString();
            String lianXiRenZhengJianLeiXing = jsonFormData.get("LianXiRenZhengJianLeiXing")==null? null:jsonFormData.get("LianXiRenZhengJianLeiXing").toString();
            String yeWuMingChen = jsonFormData.get("YeWuMingChen")==null? null:jsonFormData.get("YeWuMingChen").toString();
            String gongZuoZhiWu = jsonFormData.get("GongZuoZhiWu")==null? null:jsonFormData.get("LianXiRenZhengJianLeiXing").toString();
            String lianXiRenGongZuoDanWei = jsonFormData.get("LianXiRenGongZuoDanWei")==null? null:jsonFormData.get("LianXiRenGongZuoDanWei").toString();
            String tongXunDiZhi = jsonFormData.get("TongXunDiZhi")==null? null:jsonFormData.get("TongXunDiZhi").toString();
            String formDataId = jsonFormData.get("formDataId")==null? null:jsonFormData.get("formDataId").toString();

            ArchivesDoc archivesFormData=new ArchivesDoc();
            archivesFormData.setDossierId(archivesDocDossier.getId());//
            //archivesFormData.setDossierNum("");
            //archivesFormData.setArchivalYear(archivesDocDossier.getArchivalYear());//年度
            archivesFormData.setPages("1");//页数
            //archivesFormData.setDutyPeople(archivesDocDossier.getDutyPeople());//责任者
            archivesFormData.setSecret("0");//密级
            archivesFormData.setTypeId(archivesDocDossier.getTypeId());
            archivesFormData.setClassId(archivesDocDossier.getClassId());
            archivesFormData.setFondsId(archivesDocDossier.getFondsId());
            archivesFormData.setState("6");//状态 0 整理 1归档审批 2归档 3 鉴定 4销毁 5回收站
            archivesFormData.setFileType("doc");//doc 件  dossier卷
            //archivesFormData.setStartTime(archivesDocDossier.getStartTime());//受理时间
            archivesFormData.setTitle(lianXiRenXingMing);//题名
            if ((jsonArrayMateralInfo.size()+1)<10){
                archivesFormData.setItemNum("00"+(jsonArrayMateralInfo.size()+1));
            }else if((jsonArrayMateralInfo.size()+1)<100){
                archivesFormData.setItemNum("0"+(jsonArrayMateralInfo.size()+1));
            }else {
                archivesFormData.setItemNum(""+(jsonArrayMateralInfo.size()+1));
            }
            archivesFormData.setSource("个人");
            //archivesFormData.setAcceptUserName(archivesDocDossier.getAcceptUserName());
            archivesDocMapper.insert(archivesFormData);
            FormData formData=new FormData(archivesFormData.getId().toString(),beiZhu,lianXiRenYouXiang,banJianShuLiang,lianXiRenDianHua,lianXiRenZhengJianHaoMa
                    ,lianXiRenXingMing,youZhengBianMa,lianXiRenZhengJianLeiXing,yeWuMingChen,gongZuoZhiWu,lianXiRenGongZuoDanWei,tongXunDiZhi,formDataId);
            formDataMapper.insert(formData);
            docNum++;
            pages++;
        }



        //获取json中businessInfo数据
        JSONObject  jsonBusinessInfo= jsonObj.getJSONObject("businessInfo");
        if(!jsonBusinessInfo.isEmpty()){
            String  acceptUserCode=jsonBusinessInfo.get("acceptUserCode")==null? null:jsonBusinessInfo.get("acceptUserCode").toString();
            String  acceptUserName=jsonBusinessInfo.get("acceptUserName")==null? null:jsonBusinessInfo.get("acceptUserName").toString();
            String  applicant=jsonBusinessInfo.get("applicant")==null? null:jsonBusinessInfo.get("applicant").toString();
            String  applyFrom=jsonBusinessInfo.get("applyFrom")==null? null:jsonBusinessInfo.get("applyFrom").toString();
            String  applySubject=jsonBusinessInfo.get("applySubject")==null? null:jsonBusinessInfo.get("applySubject").toString();
            String  assort=jsonBusinessInfo.get("assort")==null? null:jsonBusinessInfo.get("assort").toString();
            String  StringbsNum=jsonBusinessInfo.get("StringbsNum")==null? null:jsonBusinessInfo.get("StringbsNum").toString();
            String  bsuinessObjectCode=jsonBusinessInfo.get("bsuinessObjectCode")==null? null:jsonBusinessInfo.get("bsuinessObjectCode").toString();
            String  bsuinessObjectType=jsonBusinessInfo.get("bsuinessObjectType")==null? null:jsonBusinessInfo.get("bsuinessObjectType").toString();
            String  busiType=jsonBusinessInfo.get("busiType")==null? null:jsonBusinessInfo.get("busiType").toString();
            String  businessObjectCode=jsonBusinessInfo.get("businessObjectCode")==null? null:jsonBusinessInfo.get("businessObjectCode").toString();
            String  businessObjectType=jsonBusinessInfo.get("businessObjectType")==null? null:jsonBusinessInfo.get("businessObjectType").toString();
            String  currentNodeId=jsonBusinessInfo.get("currentNodeId")==null? null:jsonBusinessInfo.get("currentNodeId").toString();
            String  currentNodeName=jsonBusinessInfo.get("currentNodeName")==null? null:jsonBusinessInfo.get("currentNodeName").toString();
            String  dataId=jsonBusinessInfo.get("dataId")==null? null:jsonBusinessInfo.get("dataId").toString();
            String  flowCode=jsonBusinessInfo.get("flowCode")==null? null:jsonBusinessInfo.get("flowCode").toString();
            String  flowDefineid=jsonBusinessInfo.get("flowDefineid")==null? null:jsonBusinessInfo.get("flowDefineid").toString();
            String  formId=jsonBusinessInfo.get("formId")==null? null:jsonBusinessInfo.get("formId").toString();
            String  isSetup=jsonBusinessInfo.get("isSetup")==null? null:jsonBusinessInfo.get("isSetup").toString();
            String  itemId=jsonBusinessInfo.get("itemId")==null? null:jsonBusinessInfo.get("itemId").toString();
            String  itemName=jsonBusinessInfo.get("itemName")==null? null:jsonBusinessInfo.get("itemName").toString();
            String  lawTime=jsonBusinessInfo.get("lawTime")==null? null:jsonBusinessInfo.get("lawTime").toString();
            String  orgCode=jsonBusinessInfo.get("orgCode")==null? null:jsonBusinessInfo.get("orgCode").toString();
            String  orgName=jsonBusinessInfo.get("orgName")==null? null:jsonBusinessInfo.get("orgName").toString();
            String  passWord=jsonBusinessInfo.get("passWord")==null? null:jsonBusinessInfo.get("passWord").toString();
            String  postType=jsonBusinessInfo.get("postType")==null? null:jsonBusinessInfo.get("postType").toString();
            String  promiseTime=jsonBusinessInfo.get("promiseTime")==null? null:jsonBusinessInfo.get("promiseTime").toString();
            String  receiveNumber=jsonBusinessInfo.get("receiveNumber")==null? null:jsonBusinessInfo.get("receiveNumber").toString();
            String  regionCode=jsonBusinessInfo.get("regionCode")==null? null:jsonBusinessInfo.get("regionCode").toString();
            String  regionName=jsonBusinessInfo.get("regionName")==null? null:jsonBusinessInfo.get("regionName").toString();
            String  sceneType=jsonBusinessInfo.get("sceneType")==null? null:jsonBusinessInfo.get("sceneType").toString();
            String  state=jsonBusinessInfo.get("state")==null? null:jsonBusinessInfo.get("state").toString();
            String  submitTime=jsonBusinessInfo.get("submitTime")==null? null:jsonBusinessInfo.get("submitTime").toString();
            String  timeLimit=jsonBusinessInfo.get("timeLimit")==null? null:jsonBusinessInfo.get("timeLimit").toString();
            String  type=jsonBusinessInfo.get("type")==null? null:jsonBusinessInfo.get("type").toString();
            String  usedTime=jsonBusinessInfo.get("usedTime")==null? null:jsonBusinessInfo.get("usedTime").toString();
            String  waringTime=jsonBusinessInfo.get("waringTime")==null? null:jsonBusinessInfo.get("waringTime").toString();


            ArchivesDoc archivesDocBinfo=new ArchivesDoc();
            archivesDocBinfo.setDossierId(archivesDocDossier.getId());//
            archivesDocBinfo.setDossierNum("");
            //archivesDocBinfo.setArchivalYear(archivesDocDossier.getArchivalYear());//年度
            archivesDocBinfo.setPages("1");//页数
            //archivesDocBinfo.setDutyPeople(archivesDocDossier.getDutyPeople());//责任者
            archivesDocBinfo.setSecret("0");//密级
            archivesDocBinfo.setTypeId(archivesDocDossier.getTypeId());
            archivesDocBinfo.setClassId(archivesDocDossier.getClassId());
            archivesDocBinfo.setFondsId(archivesDocDossier.getFondsId());
            archivesDocBinfo.setState("6");//状态 0 整理 1归档审批 2归档 3 鉴定 4销毁 5回收站
            archivesDocBinfo.setFileType("doc");//doc 件  dossier卷
            //archivesDocBinfo.setStartTime(archivesDocDossier.getStartTime());//受理时间
            archivesDocBinfo.setTitle("事项信息");//题名
            if ((jsonArrayMateralInfo.size()+2)<10){
                archivesDocBinfo.setItemNum("00"+(jsonArrayMateralInfo.size()+2));
            }else if((jsonArrayMateralInfo.size()+2)<100){
                archivesDocBinfo.setItemNum("0"+(jsonArrayMateralInfo.size()+2));
            }else {
                archivesDocBinfo.setItemNum(""+(jsonArrayMateralInfo.size()+2));
            }
            archivesDocBinfo.setSource("详情");
            //archivesDocBinfo.setAcceptUserName(acceptUserName);
            archivesDocMapper.insert(archivesDocBinfo);
            BusinessInfo businessInfo=new BusinessInfo(archivesDocBinfo.getId().toString(),acceptUserCode,acceptUserName,applicant,applyFrom
                    ,applySubject,assort,StringbsNum,bsuinessObjectCode,bsuinessObjectType,busiType,businessObjectCode,businessObjectType
                    ,currentNodeId,currentNodeName,dataId,flowCode,flowDefineid,formId,isSetup,itemId,itemName,lawTime,orgCode,orgName
                    ,passWord,postType,promiseTime,receiveNumber,regionCode,regionName,sceneType,state,submitTime,timeLimit,type,usedTime
                    ,waringTime);
            businessInfoMapper.insert(businessInfo);
            docNum++;
            pages++;
        }


        return pages+","+docNum;
    }


    public void downloadFile(String docId,String fileName,String savePath){
        try {
            String key = "密钥";
            StringBuffer content = new StringBuffer();
            content.append(docId);
            content.append(":");
            content.append((System.currentTimeMillis()+(3*60*60*1000)));
            //aes加密
            String token = AseEncryptUtil.aesEncrypt(content.toString(),key);
            //异或加密
            String decryptToken=AseEncryptUtil.encrypt(token);
            //System.out.println("生成token"+decryptToken);
        //fileName=fileName.trim();
        //下载文件
        try {
            /*String fileUrl=wsUrl+"WebDiskServerDemo/doc?doc_id="+docId+"&appCode=INSPUR-DZZW-YUNPAN" +
                    "&token="+decryptToken;*/
            String fileUrl="http://59.195.128.204:8680/WebDiskServerDemo/doc?doc_id="+docId;
            DownFileUtil.downLoadFromUrl(fileUrl,fileName,savePath);

        }catch (IOException io){
            io.printStackTrace();
        }


        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private String getTypeName(String type){
        String typeName="未知类型";
        switch (type){
            case "XK": typeName="行政许可"; break;
            case "JL": typeName="行政奖励"; break;
            case "QT": typeName="其他类别事项"; break;
            case "QR": typeName="行政确认"; break;
            case "CJ": typeName="行政裁决"; break;
            case "JF": typeName="行政给付"; break;
            case "BM": typeName="便民服务"; break;
            default: typeName="未知类型";
        }

        return typeName;
    }

    /**
     * @description: 挂载附件
     *
     * @author: lig
     * @date: 2022/8/19
     */
    public void upLoad(String filePath,ArchivesDoc archivesDoc){
        //String fileName= FileBaseUtil.getPathFileName(filePath.replace("\\","/"));
        //插入件数据
        archivesDocMapper.insert(archivesDoc);

        File file=new File(filePath);
        MultipartFile multipartFile= FileBaseUtil.getMultipartFile(file);
        String fileName=file.getName();
        String extName=FileBaseUtil.getFileExtName(file);

        String ossDir = RedisUtil.getSysConfig(SystemConfigType.OSS_DIR);

        Result<String> upload = uploadService.localUpload("ws", archivesDoc.getId()+"", multipartFile);

        if (org.apache.commons.lang3.StringUtils.isNotEmpty(upload.getData())) {
            File downFile = new File(ossDir + upload.getData());
            String get32uuid = IdWorker.get32UUID();
            //外部接收目录
//          String directory = "D:/a/zipPage/";
            String directory = "ws/" + get32uuid + "/";

            System.out.println("插入附件开始！！！！！！");

            //附件上传
            ArchivesDocFile archivesDocFile = new ArchivesDocFile();

            archivesDocFile.setDocId(archivesDoc.getId());
            archivesDocFile.setFileUrl(upload.getData());
            archivesDocFile.setItemNum(archivesDoc.getItemNum());
            archivesDocFile.setExtName(extName);
            archivesDocFile.setCreateTime(LocalDateTime.now());
            //archivesDocFile.setIsRealName(true);

            //挂接文件
            Result<String> mountFile = mountService.baseMountFile(archivesDocFile);

            if("1000".equals(mountFile.getCode())){
                UpdateWrapper<ArchivesDocFile> fileUpdateWrapper=new UpdateWrapper<>();
                fileUpdateWrapper.eq("docId",archivesDoc.getId());
                ArchivesDocFile archivesDocFile1=new ArchivesDocFile();
                archivesDocFile1.setFileName(fileName);
                fileMapper.update(archivesDocFile1,fileUpdateWrapper);
            }

        }

    }


    /**
     * 判断文件是否损坏
     * @param file
     * @return
     */
    private boolean getvalidate(File file){
        try{
            Tika tika = new Tika();
            String str= tika.parseToString(file);
            if(str.contains("0002")){
                return false;
            }else{
                return true;
            }
        }catch (TikaException tikaException){
            tikaException.printStackTrace();
            return false;
        }catch (IOException io){
            io.printStackTrace();
            return false;
        }
    }

    private void createZbClass(String typeId,String fondsId,ZbWbsTree zbWbsTree){
        //System.out.println("上级id树:"+zbWbsTree.getWbsTreeId()+"  id:"+zbWbsTree.getId()+"    名称:"+zbWbsTree.getIndexName());
        QueryWrapper<ArchivesClass> classQueryWrapper=new QueryWrapper<>();
        classQueryWrapper.eq("typeId",typeId);
        if("1329".equals(zbWbsTree.getParentId())){
            classQueryWrapper.eq("extId","10");
        }else if("68".equals(zbWbsTree.getParentId())){
            classQueryWrapper.eq("extId","67");
        }else if("249".equals(zbWbsTree.getParentId())){
            classQueryWrapper.eq("extId","222");
        }else {
            classQueryWrapper.eq("extId",zbWbsTree.getParentId());
        }

        ArchivesClass aClassParent =archivesClassMapper.selectOne(classQueryWrapper);

        if(aClassParent==null){
            return ;
        }

        QueryWrapper<ArchivesClass> classMaxWrapper=new QueryWrapper<>();
        classMaxWrapper.select("max(CONVERT(sort, UNSIGNED)) sort");
        classMaxWrapper.eq("typeId",typeId);
        classMaxWrapper.eq("parentId",aClassParent.getId());

        int sort=0;
        ArchivesClass aclassMax =archivesClassMapper.selectOne(classMaxWrapper);
        if(aclassMax!=null){
            sort=aclassMax.getSort();
        }
        sort++;
        ArchivesClass entity=new ArchivesClass();
        entity.setExtId(zbWbsTree.getId());
        entity.setClassName(zbWbsTree.getIndexName());
        entity.setFondsId(fondsId);
        entity.setTypeId(typeId);
        entity.setIsCatalog("1");
        entity.setClassType("1");
        entity.setParentId(aClassParent.getId());
        entity.setTemplate("dataList");
        entity.setSort(sort);
        if(sort>0 && sort < 10){
            entity.setClassNum("0"+sort);
        }else{
            entity.setClassNum(sort+"");
        }
        entity.setTreeStr(aClassParent.getTreeStr()+"·"+entity.getClassNum());

        archivesClassMapper.insert(entity);
    }

}
